package com.yidu.labyrinth.controller;

import com.yidu.labyrinth.service.LabyrinthUserService;
import com.yidu.labyrinth.vo.LabyrinthVo;
import com.yidu.labyrinth.vo.Pages;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @描述：
 * @ClassName:labyrinth
 * @backage:com.yidu.labyrinth.controller
 * @Date:2019 2019/9/25 15:27
 * @Author:易嵘
 **/
@SuppressWarnings("ALL")
@Controller
@RequestMapping("/user")
public class LabyrinthUserController {

    @Resource
    private LabyrinthUserService labyrinthUserService;

    /**
     * 查询所有（分页）
     * @return
     */
    @ResponseBody
    @RequestMapping("findAll")
    public Map<String,Object> findAll(LabyrinthVo labyrinthVo, Pages pages){
        Map<String,Object> map=new HashMap<>();
        List<LabyrinthVo> labyrinthVos=labyrinthUserService.findAll(labyrinthVo,pages);
        pages.setOverRows(labyrinthUserService.userCount(labyrinthVo));
        map.put("data",labyrinthVos);
        map.put("code",0); // 放入 code ，0
        map.put("msg", ""); // 放入msg “”
        map.put("count",pages.getOverRows()); // 放入总行数
        return map;
    }

    /**
     * 单个查询
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("findOne")
    public Map<String,Object> findOne(String userId){
        return labyrinthUserService.findById(userId);
    }

    /**
     * 增加Or修改
     * @param labyrinthVo 前台表单的值
     * @return 回执函数
     */
    @ResponseBody
    @RequestMapping("addOrUpdate")
    public int addOrUpdate(LabyrinthVo labyrinthVo){
        int rows=0;
        if(StringUtils.isEmpty(labyrinthVo.getUserId())){
            rows=labyrinthUserService.add(labyrinthVo);
        }else{
            rows=labyrinthUserService.update(labyrinthVo);
        }
        return rows;
    }

    /**
     * 修改是否有效
     * @return
     */
    @ResponseBody
    @RequestMapping("result")
    public int result(LabyrinthVo labyrinthVo){
        return labyrinthUserService.result(labyrinthVo);
    }

    /**
     * 查询所有的角色
     * @return
     */
    @ResponseBody
    @RequestMapping("roleAll")
    public List<LabyrinthVo> roleAll(){
        return labyrinthUserService.findAllRole();
    }

    /**
     * 用户登录
     * @param userName
     * @param userPwd
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("login")
    public int login(String userName, String userPwd, HttpServletRequest request){
        return labyrinthUserService.login(userName,userPwd,request);
    }

    /**
     * 转换日期格式
     * @param binder
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("yyyy-MM-dd"),true));
    }
}

